<?php
namespace Category\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class SubCategoryTable 
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addSubCategory($sub_cat_name,$p_cat_id)
    {
		$data = array(
			'sub_category_name'  => ucfirst($sub_cat_name),					
			'parent_cat_id' 	 => $p_cat_id,					
			'status'             => 1,
			'added_date' 	     => date('Y-m-d H:i:s'),
			'updated_date' 	     => date('Y-m-d H:i:s'),	
		);	
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;
    }
	public function checkCategory($cat_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('parent_cat_id="'.$cat_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();	  
	}
	public function deleteCategory($category_id){
		$deleteid=$this->tableGateway->delete(array('(parent_cat_id IN ('.$category_id.'))'));
		return $deleteid;
	} 
	public function subCategories($cat_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('parent_cat_id="'.$cat_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	  
	}
	public function getallSubCategories(){
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	        
	} 
}